/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 				</p>
 * <p>		    All right reserved. 		     		</p>
 * <p>项目名称 ：深圳公路信息资源整合及国省检日常化监管管理        				</p>
 * <p>创建者 ：詹春贤 										</p>
 * <p>描   述  ：路政管理 - 治超管理 - 非现场执法			 			</p>
 * <p>最后修改 ：$: 2017年9月4日-上午10:33:38 v 1.0.0	 Administrator   $   </p>
 * 
*/

package com.pengtu.gsj.controller.checker.lzgl.overloadMan;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pengtu.gsj.controller.base.CheckTableBaseController;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.OffSiteEnforService;
import com.pengtu.gsj.utils.LoadDataUtils;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * OffSiteEnforController
 * 
 * 2017年9月4日 上午10:33:38
 * 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("offSiteEnfor")
public class OffSiteEnforController extends CheckTableBaseController{

	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public OffSiteEnforService offSiteEnforService;
	@Autowired
	public DownService downService;
	
	@Token(save = true)
	@RequestMapping("show") 
	public String offSiteEnfor(Model model){
		//获取提示信息并返回到页面显示
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "check/lzgl/overloadMan/offSiteEnfor";
	}
	
	/**
	 * getSearchList:
	 * 适用:ajax请求，根据页面的搜索条件检索数据
	 * @param checkTable
	 * @param checkProject
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	*/
	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public Map<String, Object> getSearchList(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, @ModelAttribute Sysmenu sysmenu) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String tag = sysmenu == null ? "" : sysmenu.getTag();
		map = offSiteEnforService.getOffSiteEnfors(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, tag);
		return map;
	}
	
	/**
	 * saveOffSiteEnfor:
	 * 适用:保存非现场执法检查表内容，包括搜索条件的存储
	 * @param checkTable
	 * @param checkProject
	 * @param attributes
	 * @return 绑定页面数据（left  parentId projectId menuId） 重定向到检查表页面
	 * @exception 
	 * @since  1.0.0
	*/
	@Token(remove = true)
	@RequestMapping("save")
	public String saveOffSiteEnfor(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, RedirectAttributes attributes){
		checkTable.setCheckProject(checkProject);
		checkTable.setTableFull(MathsUtils.sum(checkTable.getYearFull(), checkTable.getYearPart(), checkTable.getPoitMark(), checkTable.getReserveMark())); //设置当前检查表格的总分
		checkTableService.save(checkTable);
		attributesParams(checkTable, attributes);
		return "redirect:/offSiteEnfor/show.do";
	}
	
	/**
	 * 
	 * downWordAndZip:
	 * 适用:检查表的一键导出，包括检查表的内容及检查条件所对应的附件
	 * @param checkProject
	 * @param checkTable
	 * @param sysmenu
	 * @return  返回打包好的压缩包供用户下载
	 * @throws Exception 
	 * @exception
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("downWord")
	public ResponseEntity<byte[]> downWordAndZip(@ModelAttribute CheckProject checkProject, @ModelAttribute CheckTable checkTable, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String tag = sysmenu == null ? "" : sysmenu.getTag();
		String sysName = sysmenu == null ? "" : sysmenu.getName();
		resultMap =  offSiteEnforService.getOffSiteEnfors(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, tag);
		return downService.downWordAndZip(SpringMvcUtils.getParameter("checkSubmoduleName"), SpringMvcUtils.getParameter("checkModuleName"), sysName, "roadAdmin/非现场执法.xml", resultMap);
	}
	
	
	/**
	 * offSiteData:
	 * 适用:页面进行条件搜索
	 * @param projectId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	*/
	@ResponseBody
	@RequestMapping(value = "offSiteData")
	public List<Map<String, Object>> offSiteData(String projectId){
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<GovernSpeed> oList = Lists.newArrayList();
		oList = LoadDataUtils.getGovernSpeeds(projectId);
		for (GovernSpeed governSpeed : oList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", governSpeed.getId());
			map.put("pId", 0);
			map.put("name", governSpeed.getStationName());
			mapList.add(map);
		}
		return(mapList);
	}
}
